// 模板 显示Hello World
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "raylib.h"
#include "../engine/include/texture.h"
#include "../engine/include/text.h"


// 游戏状态
typedef enum GAME_STATE {
    GAME_STATE_START,       // 开始等待
    GAME_STATE_LINE,        // 点击线段增长
    GAME_STATE_SURROUND,    // 绕地球
    GAME_STATE_SETTLE       // 结算
} GAME_STATE;


// 地球纹理
Texture textEarth;

// 圆心位置
float cx, cy;
// 圆的半径
float radius = 70.0f;

// 角度
float angle = 0.0f;

// 总线段长度
float totalLineLength = 0.0f;
// 当前线段长度
float lineLength = 0.0f;

// 游戏状态
GAME_STATE gameState = GAME_STATE_START;

// 初始化
void init();
// 加载纹理
void loadTextures();
// 初始化游戏
void initGame();
// 处理操作
void checkOperation(float dt);

// 绘制地球
void drawEarth();

// 绘制弧线和直线
void drawArcAndLine();

// 驱动增加线段长度
void driveIncreaseLine(float dt);

// 驱动围绕地球
void driveSurroundEarth(float dt);

// 绘制结算页面
void drawSettle();

// 主函数
int main() {
    init();
    // 游戏循环
    while (!WindowShouldClose()) {
        float dt = GetFrameTime();
        checkOperation(dt);
        BeginDrawing();
            ClearBackground(RAYWHITE);
            drawEarth();
            driveIncreaseLine(dt);
            driveSurroundEarth(dt);
            drawArcAndLine();
            drawSettle();
        EndDrawing();
    }

    CloseWindow();
    return 0;
}

void init() {
    SetConfigFlags(FLAG_WINDOW_HIGHDPI);
    InitWindow(330, 600, "Surround Earth");
    SetTargetFPS(60);
    loadTextures();
    initGame();
}

void initGame() {
    // 圆心位置
    cx = GetScreenWidth() / 2;
    cy = GetScreenHeight() / 2 + 130; // 调整圆心位置，使其在屏幕中间偏下

    angle = 0.0f;
    lineLength = 0.0f;
    totalLineLength = 0.0f;

    gameState = GAME_STATE_START;
}

void loadTextures() {
    textEarth = LoadTexture("res/earth.png");
}

void checkOperation(float dt) {
    if (gameState == GAME_STATE_START) {
        // 如果是开始状态 鼠标左键按下变为线段增长状态
        if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
            gameState = GAME_STATE_LINE;
        }
    } else if (gameState == GAME_STATE_LINE) {
        // 如果是线段增长状态 鼠标左键没按下按下变为绕地球状态
        if (!IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
            gameState = GAME_STATE_SURROUND;
            totalLineLength = lineLength; // 记录总线段长度
        }
    } else if (gameState == GAME_STATE_SETTLE) {
        if (GetKeyPressed()) {
            initGame();
        }
    }
}

void drawEarth() {
    // 绘制地球纹理
    drawTextureCenterSize(textEarth, cx, cy, WHITE, radius*2, radius*2);
}

void drawArcAndLine() {

    // 角度转换为弧度
    float theta = angle * (PI / 180.0f);
    theta = theta - PI;

    // 绘制弧线
    float ofst = 0.65f;
    float startRaius = radius-1;
    for (int i = 0; i < 4; i++) {
        float offset = i * ofst;
        DrawRingLines(
            (Vector2){cx, cy}, 
            startRaius + offset, 
            startRaius + offset + 0.25f,
            -180,
            angle - 180,
            angle,
            BLACK
        );
    }


    // 计算切点坐标
    float x0 = cx + radius * cosf(theta);
    float y0 = cy + radius * sinf(theta);

    // 计算两个方向的法向量
    float tx1 = cx - x0;
    float ty1 = cy - y0;

    // 另一个方向的法向量
    float tx2 = ty1;
    float ty2 = -tx1;

    // 归一化向量1
    float length1 = sqrtf(tx2 * tx2 + ty2 * ty2);
    tx2 /= length1;
    ty2 /= length1;

    // 计算两个方向的切线终点
    float x1 = x0 + tx2 * lineLength;
    float y1 = y0 + ty2 * lineLength;

    DrawLineEx((Vector2){x0, y0}, (Vector2){x1, y1}, 3, BLACK);
}

void driveIncreaseLine(float dt) {
    if (gameState == GAME_STATE_LINE) {
        lineLength += 200 * dt; // 每秒增加100像素
        // 最大长度为周长的2倍 超过直接进入下个阶段
        if (lineLength > 2 * 2 * PI * radius) {
            totalLineLength = lineLength = 2 * 2 * PI * radius;
            gameState = GAME_STATE_SURROUND;
        }
    }
}

void driveSurroundEarth(float dt) {
    if (gameState != GAME_STATE_SURROUND) return;

    float deltaAngle = 80 * dt; // 每秒增加60度
    angle += deltaAngle;

    // 线长需要减去本帧弧线的长度
    lineLength -= 2 * PI * radius * (deltaAngle / 360.0f);

    if (lineLength < 0) {
        lineLength = 0; // 确保线段长度不为负

        gameState = GAME_STATE_SETTLE; // 进入结算状态
    }
}


void drawSettle() {
    if (gameState != GAME_STATE_SETTLE) return;

    DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (Color){0, 0, 0, 128});
    // 显示完成度百分比
    float l = 2 * PI * radius; // 周长
    // 超过周长的要减去,且要减去超出的部分 过犹不及
    if (totalLineLength > l) {
        totalLineLength = l - (totalLineLength - l);
    }
    float completion = (totalLineLength / l) * 100.0f;
    drawTextCenter(TextFormat("Completion: %.2f%%", completion), GetScreenWidth()/2, GetScreenHeight()/2 - 100, 30, WHITE);
    drawTextCenter("Press any key to restart...", GetScreenWidth()/2, GetScreenHeight()/2, 20, WHITE);
}
